package cn.kinggm520;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 作者: kinggm Email:731586355@qq.com
 * 时间:  2020-05-14 13:35
 */
@Component
public class MyListener{

    //    监听队列
    @RabbitListener(queues = "test_queue")
    public void myListener1(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {

        System.out.println(message);
        try {


            System.out.println("消息为：>>>>>>>>>>>>"+message);
            int i=3/0;
            System.out.println("处理业务逻辑");
//        手动签收
            channel.basicAck(tag, true);

        } catch (Exception e) {
            //            拒签收 重回队列
            channel.basicNack(tag, true, false);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            }
        }


    }

}
